import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.concurrent.Callable;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-10-11
 * Time: 19:37
 */

class Card {
    public int rank; // 数值
    public String suit; // 花色

    public Card(int rank, String suit) {
        this.rank = rank; this.suit = suit;
    }



    //自定义类型一旦牵扯到比较，一定要重写comparable或者comparator接口

    @Override
    public String toString() {
        return "Card{" +
                "rank=" + rank +
                ", suit='" + suit + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        //getClass() != o.getClass != o.getClass()) return false
        if (o == null || getClass() != o.getClass()) return false;
        Card card = (Card) o;
        return rank == card.rank && Objects.equals(suit, card.suit);
    }

    @Override
    public int hashCode() {
        return Objects.hash(rank, suit);
    }
}

/*class RankComparator implements Comparator<Card> {
    @Override
    public int compare(Card o1, Card o2) {
        return o1.rank - o2.rank;
    }
}*/
public class TestDemo {

    //面试高频考点1：top - K问题
    //给你100w个数据，找到你前10个最大的元素
    //思路一：整体排序，输出前10个数据 - 最快时间复杂度为O(n*log2n);
    //思路二：建堆，如果要找前三个最大的元素，只需要弹出前三个元素即可
    //思路三：topk - 小根堆：
    //思路四：分治算法 - 兴趣看《算法导论》

    //思路三：
    //1.先把前k个元素，创建为小根堆
    //2.从第k + 1个元素开始依次比较大小，如果大于，则交换堆顶元素与最后元素，然后出队，再入队

    /*求前k个最大元素，建一个小根堆
    求前k个最小元素, 建一个大根堆
    求第k大的元素，建一个小堆，堆顶元素就是第k大的元素
    求第k小的元素，建一个大堆，堆顶元素就是第k小的元素*/

    //面试高频考点2：堆排序
    //对着一组数据 进行从小到大的排序

    //1.调整为大根堆
    //2.堆顶元素与最后一个未排序元素交换即可
    //3.end--。循环
    public static void main4(String[] args) {
        Card card1 = new Card(2,"♥");
        Card card2 = new Card(2,"♥");
        System.out.println(card1.equals(card2));
    }
    public static void main(String[] args) {
        Card card1 = new Card(2,"♥");
        Card card2 = new Card(1,"♥");
        //RankComparator rankComparator = new RankComparator();

        //内部类 - 推荐
        PriorityQueue<Card> priorityQueue = new PriorityQueue<>(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.rank - o2.rank;
            }
        });

        //lambda表达式 - 可读性差
        //PriorityQueue<Card> priorityQueue = new PriorityQueue<>((x,y)->{return x.rank - y.rank;});
        priorityQueue.offer(card1);
        priorityQueue.offer(card2);
        System.out.println(priorityQueue);
    }
    public static void main2(String[] args) {
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap();
        testHeap.createHeap(array);
        System.out.println();
        testHeap.heapSort();
        System.out.println(Arrays.toString(testHeap.elem));
    }

    public static void main1(String[] args) {
        Card card1 = new Card(2,"♥");
        Card card2 = new Card(1,"♥");

        PriorityQueue<Card> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(card1);
        priorityQueue.offer(card2);

        //System.out.println(priorityQueue);

        //System.out.println(card1.compareTo(card2));

        //RankComparator rankComparator = new RankComparator();
        //System.out.println(rankComparator.compare(card1, card2));
    }
}
